<?php

namespace Home\Controller; //当前命名空间跟文件目录一样
use Common\Common\CommonController; //引用Common目录下的Common下的CommonController
class OrderController extends CommonController
{

	//订单界面显示
	public function order()
	{
		//调用公共控制器中的检查用户身份
		$this->checkUser();
		//获取当前用户信息
		$u_name = session("u_name");
		$modleUser = D('User');
		$fieldUser = 'user_id,u_name,u_gender,mobile_phone,u_email,u_address,u_birthday'; //获取字段字符串
		$userInfo = $modleUser->relation(false)->field($fieldUser)->where(array('u_name' => $u_name))->select();

		//获取get方法传进来的商品编号gi
		$goods_id = I('get.gi');
		if (empty($goods_id)) $this->error("非法操作！", U('?m=Home&c=goods&a=index'));

		//获取商品信息
		$modelGoods = M('Goods');
		$goodsInfo = $modelGoods->where(array('goods_id' => $goods_id))->select();

		//获取商品分类信息
		$modelCategory = M('Category');
		$categoryInfo = $modelCategory->where(array('category_id' => $goodsInfo[0]['category_id']))->select();
		$this->assign('userInfo', $userInfo);
		$this->assign('goodsInfo', $goodsInfo);
		$this->assign('categoryInfo', $categoryInfo);
		$this->display();
	}

	//用户确认提交订单后
	public function centain()
	{
		//调用公共控制器中的检查用户身份
		$this->checkUser();
		//获取所有表单提交的post数据
		$orderInfo = I('post.');

		//验证手机号和商品数量
		if (checkPhone($orderInfo['u_phone']) == null && checkPosotiveInteger($orderInfo['goods_count']) == null) {
			//获取商品信息
			$modelGoods = M('Goods');
			$goodsInfo = $modelGoods->where(array('goods_id' => $orderInfo['goods_id']))->select();

			//获取商品分类信息
			$modelCategory = M('Category');
			$categoryInfo = $modelCategory->where(array('category_id' => $orderInfo['category_id']))->select();

			//生成订单所需要插入的字段
			$where = array(
				'user_id' => $orderInfo['user_id'],      //会员编号
				'shipping_user' => session('u_name'),    //收货人
				'shipping_adress' => $orderInfo['u_address'],  //收货地址
				'shipping_phone' => $orderInfo['u_phone'],    //联系电话
				'order_money' => $goodsInfo[0]['goods_price'] * $orderInfo['goods_count'],  //订单总额
				'category_id' => $categoryInfo[0]['category_id'],    //商品分类id
				'category_name' => $categoryInfo[0]['category_name'],    //商品分类名称
				'goods_id' => $orderInfo['goods_id'],   //商品id
				'goods_name' => $goodsInfo[0]['goods_name'],   //商品名称
				'goods_count' => $orderInfo['goods_count'],   //购买商品数量
			);

			//插入订单信息,返回的是受影响的行数
			if (!empty(M('Order')->add($where))) {
				$this->success("订单提交成功，可以到个人中心查看哦！", U('?m=home&c=goods&a=item&gi=' . $orderInfo['goods_id']));
				die;
			}
		}
		$this->error("发生错误，订单提交失败！", U('?m=home&c=goods&a=item&gi=' . $orderInfo['goods_id']));
	}

	//用户个人订单显示
	public function showList()
	{
		//调用公共控制器中的检查用户身份
		$this->checkUser();
		//获取当前登录用户名
		$u_name = session('u_name');
		//实例化用户关联模型
		$modelUser = D('User');
		//接收传递订单状态查询值os
		$shipping_status = I('os') != null ? I('os') : '';
		$shipping_status = ($shipping_status == 1) ? '待发货' : $shipping_status;
		$shipping_status = ($shipping_status == 2) ? '待收货' : $shipping_status;
		$shipping_status = ($shipping_status == 3) ? '已收货' : $shipping_status;

		if (!empty($shipping_status)) {
			$where = array('shipping_status' => $shipping_status);
			$user_id = $modelUser->field("user_id")->where(array('u_name' => $u_name))->find()['user_id'];
			//实例化订单模型，查询目前用户具体物流状态订单
			$orderInfo = M('Order')->where(array('user_id' => $user_id))->where($where)->select();
			//将订单数据返回到视图
			$this->assign("orderInfo", $orderInfo);
		} else {
			// print_r($where);die;
			//模型类对象获取当前用户的全部订单信息
			$orderInfo = $modelUser->relation('Order')->where(array('u_name' => $u_name))->select()[0]['Order'];
			//将订单数据返回到视图
			$this->assign("orderInfo", $orderInfo);
		}
		$this->display();
	}

	//用户确认收货，改变物流状态
	public function update()
	{
		//调用公共控制器中的检查用户身份
		$this->checkUser();
		//获取确认收货的订单编号
		$order_id = I('get.oi');
		//实例化Order模型对象
		$model = M('Order');
		//执行更新物流状态语句
		if (!empty($model->where(array('order_id' => $order_id))->save(array('shipping_status' => '已收货')))) {
			$this->success("确认收货成功！", U('?m=home&c=order&a=showList'));
			die;
		}
		$this->error("发生错误，确认收货失败！", U('?m=home&c=order&a=showList'));
	}
}
